<template>
  <el-dialog
    :title="!dataForm.originNo ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="原点名称" prop="originName">
      <el-input v-model="dataForm.originName" placeholder="原点名称"></el-input>
    </el-form-item>
    <el-form-item label="是否正在使用" prop="originIsload">
      <el-input v-model="dataForm.originIsload" placeholder="是否正在使用"></el-input>
    </el-form-item>
    <el-form-item label="左上角坐标X" prop="originX">
      <el-input v-model="dataForm.originX" placeholder="左上角坐标X"></el-input>
    </el-form-item>
    <el-form-item label="左上角坐标Y" prop="originY">
      <el-input v-model="dataForm.originY" placeholder="左上角坐标Y"></el-input>
    </el-form-item>
    <el-form-item label="左上角坐标Z" prop="originZ">
      <el-input v-model="dataForm.originZ" placeholder="左上角坐标Z"></el-input>
    </el-form-item>
    <el-form-item label="原点信息描述" prop="originReference">
      <el-input v-model="dataForm.originReference" placeholder="原点信息描述"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          originNo: 0,
          originName: '',
          originIsload: '',
          originX: '',
          originY: '',
          originZ: '',
          originReference: ''
        },
        dataRule: {
          originName: [
            { required: true, message: '原点名称不能为空', trigger: 'blur' }
          ],
          originIsload: [
            { required: true, message: '是否正在使用不能为空', trigger: 'blur' }
          ],
          originX: [
            { required: true, message: '左上角坐标X不能为空', trigger: 'blur' }
          ],
          originY: [
            { required: true, message: '左上角坐标Y不能为空', trigger: 'blur' }
          ],
          originZ: [
            { required: true, message: '左上角坐标Z不能为空', trigger: 'blur' }
          ],
          originReference: [
            { required: true, message: '原点信息描述不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.originNo = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.originNo) {
            this.$http({
              url: this.$http.adornUrl(`/origin/origin/info/${this.dataForm.originNo}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.originName = data.origin.originName
                this.dataForm.originIsload = data.origin.originIsload
                this.dataForm.originX = data.origin.originX
                this.dataForm.originY = data.origin.originY
                this.dataForm.originZ = data.origin.originZ
                this.dataForm.originReference = data.origin.originReference
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/origin/origin/${!this.dataForm.originNo ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'originNo': this.dataForm.originNo || undefined,
                'originName': this.dataForm.originName,
                'originIsload': this.dataForm.originIsload,
                'originX': this.dataForm.originX,
                'originY': this.dataForm.originY,
                'originZ': this.dataForm.originZ,
                'originReference': this.dataForm.originReference
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
